﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

namespace sistemaSaoJudasTadeu.DynamicData.CustomPages.disciplinas
{
    public partial class professoresDisciplina : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            //Pegar a descricao da serie

            string sqlSerie = "select * from serie where codigoSerie = " + Request["codigoSerie"].ToString();

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            SqlCommand cmdSerie = new SqlCommand(sqlSerie, sqlc);
            SqlDataReader myReaderSerie = cmdSerie.ExecuteReader();

            while (myReaderSerie.Read())
            {
                lblDescricaoSerie.Text = myReaderSerie["descricaoSerie"].ToString();
            }

            cmdSerie.Dispose();
            myReaderSerie.Close();
                      

            //Pegar a descricao da disciplina

            lblDescricaoDisciplina.Text = Request["descricaoDisciplina"].ToString();

            sqlc.Close();  

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            SqlConnection sqlc = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc.Open();

            //deletar todos os professores da disciplina

            string sqlDeletar = "delete from disciplinaProfessor where codigoDisciplina =" + Request["codigoDisciplina"].ToString();
            SqlDataReader myReader = null;

            SqlCommand cmdDel = new SqlCommand(sqlDeletar, sqlc);

            myReader = cmdDel.ExecuteReader();
            cmdDel.Dispose();

            //cadastrar os professores que estavam marcados nos checkbox

            //loop em professores

            SqlConnection sqlc2 = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
            sqlc2.Open();

            string sqlProfessores = "select codigoProfessor, nomeProfessor from professor order by nomeProfessor";          

            SqlCommand cmdProfessor = new SqlCommand(sqlProfessores, sqlc2);
            SqlDataReader myReaderProfessor = cmdProfessor.ExecuteReader();

            while (myReaderProfessor.Read())
            {


                string valorRec = Request[myReaderProfessor["codigoProfessor"].ToString()];

                if ( valorRec == "on") { 
                
                    //inserir registro

                    SqlConnection sqlc3 = new SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                    sqlc3.Open();

                    string insProfessorDisciplina = "insert into disciplinaProfessor(codigoProfessor, codigoDisciplina) values (" + myReaderProfessor["codigoProfessor"].ToString() + "," + Request["codigoDisciplina"].ToString() + ")";

                    SqlCommand cmdIns = new SqlCommand(insProfessorDisciplina, sqlc3);
                    SqlDataReader myReaderIns = cmdIns.ExecuteReader();

                    cmdIns.Dispose();

                    sqlc3.Close();

                }
            }

            sqlc.Close();
            sqlc2.Close();
            myReaderProfessor.Close();


            Response.Redirect("professoresDefinidos.aspx");

        }
    }
}
